﻿using DotNetCore.CAP;
using DotNetCore.CAP.Messages;
using Framework.ExtensionsMods.RemoteRequest;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Net.Mime;
using System.Reflection.PortableExecutable;

namespace Framework.ExtensionsMods.Test.Controllers
{
	/// <summary>
	/// CAP 使用示例
	/// </summary>
	[ApiController]
	[Route("[controller]")]
	public class PublishController : ControllerBase
	{
		private readonly ICapPublisher _capBus;
		private readonly HttpHelper _httpHelper;

		public PublishController(HttpHelper httpHelper, ICapPublisher capPublisher)
		{
			_capBus = capPublisher;
			_httpHelper = httpHelper;
		}

		public class JsonRootDto
		{
			/// <summary>
			/// 
			/// </summary>
			[JsonProperty("statusCode")]
			public int StatusCode { get; set; }

			/// <summary>
			/// 
			/// </summary>
			[JsonProperty("data")]
			public Data Data { get; set; }

			/// <summary>
			/// 
			/// </summary>
			[JsonProperty("succeeded")]
			public bool Succeeded { get; set; }

			/// <summary>
			/// 
			/// </summary>
			[JsonProperty("errors")]
			public string Errors { get; set; }

			/// <summary>
			/// 
			/// </summary>
			[JsonProperty("extras")]
			public string Extras { get; set; }

			/// <summary>
			/// 
			/// </summary>
			[JsonProperty("timestamp")]
			public int Timestamp { get; set; }
		}

		public class Data
		{
			/// <summary>
			/// 
			/// </summary>
			[JsonProperty("token")]
			public string Token { get; set; }
		}

		/// <summary>
		/// Http远程请求测试
		/// </summary>
		/// <returns></returns>
		[HttpGet]
		[Route("HttpTest")]
		public async Task<string> HttpTest()
		{
			string tmpGet = await _httpHelper.HttpGetAsync("http://www.baidu.com",new HttpItem() );

			HttpItem item = new HttpItem();
			item.Accept = "application/json";

			//var tmpPost = await _httpHelper.HttpPostAsync<JsonRootDto>("http://localhost:19001/api/app/account/login", item);

			//item.Header.Add("authorization", "Bearer " + tmpPost.Data.Token);
			//tmpGet = await _httpHelper.HttpGetAsync("http://localhost:19001/api/app/hello-world?name=123", item);

			item = new HttpItem();
		
			var tmpPost2 = await _httpHelper.HttpGetAsync("http://cloud.jxnav.com:5000/api/device-newdata/list", item);

			item.Accept = "text/plain";
			item.ContentType = "application/json";
			item.Postdata = "{\r\n  \"buoysNo\": \"01\",\r\n  \"startTime\": \"2025-05-27 10:30:00\"\r\n}";

			var tmpPost1 = await _httpHelper.HttpPostAsync("http://cloud.jxnav.com:5000/api/device-historydata/list", item);

			return tmpPost1;
		}

		/// <summary>
		/// 发送消息
		/// </summary>
		/// <returns></returns>
		[HttpGet]
		[Route("Send")]
		public string SendMessage()
		{
			_capBus.Publish("test.show.time", DateTime.Now);

			return "Ok";
		}

		/// <summary>
		/// 发送延迟消息
		/// </summary>
		/// <returns></returns>
		[HttpGet]
		[Route("DelaySend")]
		public string SendDelayMessage()
		{
			_capBus.PublishDelay(TimeSpan.FromSeconds(100), "test.show.time", DateTime.Now);

			return "Ok";
		}

		/// <summary>
		/// 发送包含头信息的消息
		/// </summary>
		/// <returns></returns>
		[HttpGet]
		[Route("SendHeader")]
		public string SendHeaderMessage()
		{
			var header = new Dictionary<string, string>()
			{
				["my.header.first"] = "first",
				["my.header.second"] = "second"
			};

			_capBus.Publish("test.show.time.header", DateTime.Now, header);

			return "Ok";
		}

		/// <summary>
		/// 处理消息
		/// </summary>
		/// <param name="time"></param>
		[NonAction]
		[CapSubscribe("test.show.time")]
		public void ReceiveMessage(DateTime time)
		{
			Console.WriteLine("message time is:" + time);
		}

		/// <summary>
		/// 处理包含头信息的消息
		/// </summary>
		/// <param name="time"></param>
		/// <param name="header"></param>
		[NonAction]
		[CapSubscribe("test.show.time.header")]
		public void ReceiveMessage(DateTime time, [FromCap] CapHeader header)
		{
			Console.WriteLine("message time is:" + time);
			Console.WriteLine("message firset header :" + header["my.header.first"]);
			Console.WriteLine("message second header :" + header["my.header.second"]);
		}
	}
}
